package com.lm.scau.dyb;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.lm.scau.dyb.Receiver.NetworkChangeReceiver;
import com.lm.scau.dyb.activity.base.BaseActivity;
import com.lm.scau.dyb.fragment.HomeFragment;
import com.lm.scau.dyb.fragment.NoticeFragment;
import com.lm.scau.dyb.fragment.TextFragment;
import com.lm.scau.dyb.fragment.TitleFragment;
import com.lm.scau.dyb.fragment.UserFragment;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends BaseActivity {

    private HomeFragment homeFragment; //首页
    private TitleFragment titleFragment; //标题栏
    private TextFragment textFragment; //广场
    private NoticeFragment noticeFragment; //公告
    private UserFragment userFragment; //我的

    private RelativeLayout homeLayout;
    private RelativeLayout noticeLayout;
    private RelativeLayout textLayout;
    private RelativeLayout userLayout;
    private Map<String, Fragment> fragmentMap; //给fragment加入缓存

    private NetworkChangeReceiver netWorkReceiver;
    private long mExitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentMap = new HashMap<>();
        initView();
        initAll();
        setDefaultFragment();
    }

    private void initAll() {
        //开启广播
        registerBroadcast();
        //开启接收推送的线程
        //开始跟新数据库的东西
    }

    private void registerBroadcast() {
        // 网络状况监听的广播
        if (null == netWorkReceiver) {
            netWorkReceiver = new NetworkChangeReceiver();
            IntentFilter networkFilter = new IntentFilter();
            networkFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            registerReceiver(netWorkReceiver, networkFilter);
        }
    }

    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        titleFragment = (TitleFragment) fm.findFragmentById(R.id.title_fragment);
        homeFragment = new HomeFragment();
        fragmentMap.put("首页", homeFragment);
        titleFragment.initTitle(true, true, "首页");
        resetBottomUI(true, false, false, false);
        transaction.replace(R.id.content, homeFragment);
        transaction.commit();

    }

    private void initView() {
        homeLayout = (RelativeLayout) findViewById(R.id.bottom_home);
        textLayout = (RelativeLayout) findViewById(R.id.bottom_faxian);
        noticeLayout = (RelativeLayout) findViewById(R.id.bottom_notice);
        userLayout = (RelativeLayout) findViewById(R.id.bottom_me);
        homeLayout.setOnClickListener(new mOnclickListener());
        textLayout.setOnClickListener(new mOnclickListener());
        noticeLayout.setOnClickListener(new mOnclickListener());
        userLayout.setOnClickListener(new mOnclickListener());
    }

    private void resetBottomUI(boolean homeHighlight, boolean textHighlight, boolean noticeHighlight, boolean userHighlight) {
        homeLayout.setSelected(homeHighlight);
        textLayout.setSelected(textHighlight);
        noticeLayout.setSelected(noticeHighlight);
        userLayout.setSelected(userHighlight);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                showToast("再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
//                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unRegisterReceiver();
    }

    private void unRegisterReceiver() {
        if (null != netWorkReceiver) {
            unregisterReceiver(netWorkReceiver);
        }
    }

    class mOnclickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            switch (view.getId()) {
                case R.id.bottom_home:
                    titleFragment.initTitle(true, true, "首页");
                    resetBottomUI(true, false, false, false);
                    homeFragment = (HomeFragment) fragmentMap.get("首页");
                    if (null == homeFragment) {
                        homeFragment = new HomeFragment();
                        fragmentMap.put("首页", homeFragment);
                    }
                    transaction.replace(R.id.content, homeFragment);
                    break;
                case R.id.bottom_faxian:
                    titleFragment.initTitle(true, false, "发现");
                    resetBottomUI(false, true, false, false);
                    textFragment = (TextFragment) fragmentMap.get("发现");
                    if (null == textFragment) {
                        textFragment = new TextFragment();
                        fragmentMap.put("发现", textFragment);
                    }
                    transaction.replace(R.id.content, textFragment);
                    break;
                case R.id.bottom_notice:
                    titleFragment.initTitle(true, true, "公告");
                    resetBottomUI(false, false, true, false);
                    noticeFragment = (NoticeFragment) fragmentMap.get("公告");
                    if (null == noticeFragment) {
                        noticeFragment = new NoticeFragment();
                        fragmentMap.put("公告", noticeFragment);
                    }
                    transaction.replace(R.id.content, noticeFragment);
                    break;
                case R.id.bottom_me:
                    titleFragment.initTitle(true, true, "我的");
                    resetBottomUI(false, false, false, true);
                    userFragment = (UserFragment) fragmentMap.get("我的");
                    if (null == userFragment) {
                        userFragment = new UserFragment();
                        fragmentMap.put("我的", userFragment);
                    }
                    transaction.replace(R.id.content, userFragment);
                    break;
                default:
                    break;
            }
            transaction.commit();
        }
    }
}
